package com.ainiyar.springweb.util;

import com.wf.captcha.*;
import com.wf.captcha.base.Captcha;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;


/**************************************
 * File Name : CaptchaUtil
 * Created with : IntelliJ IDEA.
 * Author : 艾尼亚尔·毛拉吾提
 * QQ : 524155792  WeChat : ainiyar0124
 * Created Time : 2024/2/6  18:14
 * Desc :验证码工具类
 ***************************************/
@Data
public class CaptchaUtil {
    /**
     * 验证码类型
     * ARITHMETIC:算数 CHINESE:中文 CHINESE_GIF:中文闪图   GIF|SPEC:闪图
     */
    private static final String codeType = "ARITHMETIC";
    /**
     * 验证码内容长度
     */
    private static final int length = 4;
    /**
     * 验证码宽度
     */
    private static final int width = 160;
    /**
     * 验证码高度
     */
    private static final int height = 36;
    /**
     * 字体大小
     */
    private static final int fontSize = 25;
    public static Map<String,Object> getCaptcha(){
        String uuid = "code-key-"+ UUID.randomUUID();
        Captcha captcha = generateCaptcha();
        //当验证码类型为 arithmetic时且长度 >= 2 时，captcha.text()的结果有几率为浮点型
        String captchaValue = captcha.text();
//        if(Objects.equals(codeType, "ARITHMETIC") && captchaValue.contains(".")){
//            captchaValue = captchaValue.split("\\.")[0];
//        }
        // 验证码信息
        return new HashMap<String,Object>(3){{
            put("img",captcha.toBase64());
            put("value", captchaValue);
            put("key",uuid);
        }};
    }
    private static Captcha generateCaptcha() {
        Captcha captcha = null;
        switch (codeType) {
            case "ARITHMETIC":
                captcha = new FixedArithmeticCaptcha(width, height);
                captcha.setLen(length);
                break;
            case "CHINESE":
                captcha = new ChineseCaptcha(width, height);
                captcha.setLen(length);
                break;
            case "CHINESE_GIF":
                captcha = new ChineseGifCaptcha(width, height);
                captcha.setLen(length);
                break;
            case "GIF":
                captcha = new GifCaptcha(width, height);
                captcha.setLen(length);
                break;
            case "SPEC":
                captcha = new SpecCaptcha(width, height);
                captcha.setLen(length);
        }
        return captcha;
    }

    static class FixedArithmeticCaptcha extends ArithmeticCaptcha {
        public FixedArithmeticCaptcha(int width, int height) {
            super(width, height);
        }

        @Override
        protected char[] alphas() {
            // 生成随机数字和运算符
            int n1 = num(1, 10), n2 = num(1, 10);
            int opt = num(3);

            // 计算结果
            int res = new int[]{n1 + n2, n1 - n2, n1 * n2}[opt];
            // 转换为字符运算符
            char optChar = "+-x".charAt(opt);

            this.setArithmeticString(String.format("%s%c%s=?", n1, optChar, n2));
            this.chars = String.valueOf(res);

            return chars.toCharArray();
        }
    }
}

